import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
import { resolve } from 'path'

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd())
  return {
    plugins: [react()],
    base: "./",
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src')
      }
    },
    server: {
      host: "0.0.0.0",
      port: 3000,
      proxy: {
        '/api': {
          target: env.VITE_LLM_API_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
          configure: (proxy, options) => {
            proxy.on('proxyReq', (proxyReq, req) => {
              console.log('原始请求:', req.url);
              console.log('代理后地址:', `${proxyReq.protocol}//${proxyReq.host}${proxyReq.path}`);
            });
          }
        },
        '/ollama': {
          target: env.VITE_LLM_API_URL,
          changeOrigin: true,
          configure: (proxy, options) => {
            proxy.on('proxyReq', (proxyReq, req) => {
              console.log('原始请求:', req.url);
              console.log('代理后地址:', `${proxyReq.protocol}//${proxyReq.host}${proxyReq.path}`);
            });
          }
        }
      }
    },
    build: {
      rollupOptions: {
        output: {
          entryFileNames: 'assets/[name].js',
          chunkFileNames: 'assets/[name].js',
          assetFileNames: 'assets/[name].[ext]'
        }
      }
    }

  }
})
